package com.wuxiaolong.tools.controller;

import com.wuxiaolong.tools.constant.BusinessException;
import com.wuxiaolong.tools.constant.ResultCode;
import com.wuxiaolong.tools.model.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * controller 增强器
 * 用于 1.异常处理   2.初始化数据绑定器  3.绑定到Model中
 */
@RestControllerAdvice
@Slf4j
public class AdvicerController {

    /**
     * 业务异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    public JsonResult handleBusinessException(BusinessException e){
        log.error("业务异常:code:{},msg:{}",e.getCode(),e.getMsg());
        log.error("{}",e);
        e.printStackTrace();
        return new JsonResult(e.getCode(),e.getMsg());
    }

    /**
     * 未知异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public JsonResult handleException(Exception e){
        log.error(">>>>>>>>>>>>>>>>>>>>>>>>未知异常<<<<<<<<<<<<<<<<<<<<<<<<<<");
        log.error("{}",e);
        e.printStackTrace();
        return new JsonResult(ResultCode.UNKNOWN_ERR.getCode(),ResultCode.UNKNOWN_ERR.getMsg());
    }
}
